﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;

namespace AppDesigner.XamlDom
{
    public class XamlObjectServiceProvider : IServiceProvider,IProvideValueTarget
    {
        #region Constructor

        public XamlObjectServiceProvider(XamlObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            XamlObject = obj;
            Resolver = new XamlTypeResolverProvider(obj);
        }

        #endregion

        #region Property

        public XamlObject XamlObject { get; private set; }
        public XamlTypeResolverProvider Resolver { get; private set; }

        #endregion

        #region IServiceProviderImplements

        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IProvideValueTarget))
            {
                return this;
            }
            if (serviceType == typeof(IXamlTypeResolver))
            {
                return Resolver;
            }
            return null;
        }

        #endregion

        #region IProvideValueTargetImplements

        public object TargetObject
        {
            get { return XamlObject.ParentProperty.ParentObject.Instance; }
        }

        public object TargetProperty
        {
            get { return XamlObject.ParentProperty.DependencyProperty; }
        }

        #endregion
      
    }
}
